package com.twang.stock.email;

import com.twang.stock.enmu.CommonEnum;
import com.twang.stock.entity.StockWarningEvent;
import com.twang.stock.entity.User;
import com.twang.stock.mapper.StockWarningEventMapper;
import com.twang.stock.mapper.UserMapper;
import com.twang.stock.service.jinyan.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SendmailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Autowired
    private CommonService commonService;

    @Resource
    private StockWarningEventMapper stockWarningEventMapper;
    @Resource
    private UserMapper userMapper;

    public void sendMail(String message, String name,String code,String rule) {
       // User user = userService.getUserByToken();
        List<String> toMail = Arrays.asList(commonService.getValue(CommonEnum.TO_MAIL.getCode()).split(","));
        try {
          toMail.forEach(mail -> {
                //简单邮件
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setFrom("wangtao_13140520@163.com");
                simpleMailMessage.setTo(mail);
                simpleMailMessage.setSubject("【预警】 " + name);
                simpleMailMessage.setText(message);
                mailSender.send(simpleMailMessage);
            });
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,rule);
        }
    }

    public void sendMailWithEmail(String message, String name,String code,Long userId) {
        User user=userMapper.selectByPrimaryKey(userId);
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("精研 V3.0<wangtao_13140520@163.com>");
            messageHelper.setTo(user.getEmail());
            messageHelper.setSubject("【预警】 " + name);
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>> sendMailWithEmail error {}",e);
        }finally {
            insertEvent(name,message,code,userId.toString());
        }
    }

    public void sendMailWithEmailNew(String message, String name,String code,Long userId) {
        User user=userMapper.selectByPrimaryKey(userId);
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("精研 V3.0<wangtao_13140520@163.com>");
            messageHelper.setTo(user.getEmail());
            messageHelper.setSubject("【飙升热度】 " + name);
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,userId.toString());
        }
    }

    /**
     * 生产记录
     * @param code
     * @param message
     */
    public void insertEvent(String name,String message,String code,String rule){
        try{
            StockWarningEvent event=new StockWarningEvent();
            event.setCode(name);
            event.setMessage(message);
            event.setCreateTime(new Date());
            event.setWarnTime(new Date());
            event.setRule(rule);
            event.setName(code);
            stockWarningEventMapper.insert(event);
        }catch (Exception e){

        }
    }

    public void sendMailWithByZuoT(String message, String name,String code,String eamil) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("做T预警<wangtao_13140520@163.com>");
            messageHelper.setTo(eamil);
            messageHelper.setSubject("【做T】 " + name+"!!!");
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,eamil);
        }
    }

    public void 触发分时涨幅超过(String message, String name,String code,String eamil) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("触发分时涨幅<wangtao_13140520@163.com>");
            messageHelper.setTo(eamil);
            messageHelper.setSubject("【"+ name+"】 " );
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,eamil);
        }
    }

    public void 触发成交量超过60天最高(String message, String name,String code,String eamil) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("触发超过60天量最高<wangtao_13140520@163.com>");
            messageHelper.setTo(eamil);
            messageHelper.setSubject("【"+ name+"】 " );
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,"触发成交量超过60天最高");
        }
    }

    public void 触发转债(String message, String name,String code,String eamil) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom("转债触发分时异常<wangtao_13140520@163.com>");
            messageHelper.setTo(eamil);
            messageHelper.setSubject("【"+ name+"】 " );
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }finally {
            insertEvent(name,message,code,"转债");
        }
    }

    public void News(String message, String name,String code,String eamil) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage,true,"utf-8");
            messageHelper.setFrom(name+"<wangtao_13140520@163.com>");
            messageHelper.setTo(eamil);
            messageHelper.setSubject(name);
            messageHelper.setText(message);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            log.error(">>>>>>>>>> e",e);
        }finally {
            insertEvent(name,message,code,"NEWS");
        }
    }
}
